// VideoPlayer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

using namespace std;
using namespace cv;

Mat frame;
Mat yuv;
int framecnt = 0;

void MouseCallBack(int event, int x, int y, int flags, void* userdata)
{
	uchar *data;
	unsigned char r, g, b;
	char str[80];

	if(event == EVENT_LBUTTONDOWN) {
		data = frame.ptr<uchar>(y);
		b = data[x*3];
		g = data[x*3+1];
		r = data[x*3+2];
		printf("  Image %d %d: %d,%d,%d\n", x, y, r, g, b);
	}
	else if (event == EVENT_RBUTTONDOWN) {
		//cout << "Right: " << x << "," << y << endl;
		sprintf(str, "img%04d.bmp", framecnt);
		printf("%s\n", str);
		imwrite(str, frame);
	}
	else if (event == EVENT_MBUTTONDOWN) {
		//cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
	}
	else if ( event == EVENT_MOUSEMOVE ) {
		//cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	VideoCapture cap;
	char c=0;

	//cap.open("Overwatch.avi");
	cap.open(1);
	if(!cap.isOpened()) { // check if we succeeded
		printf("cannot open video!\n");
		return -1;
	}
	//bool capset = cap.set(CAP_PROP_CONVERT_RGB, 0);
	//bool capset = cap.set(CV_CAP_PROP_CONVERT_RGB, 0);
	//printf("CV_CAP_PROP_CONVERT_RGB: %d, %d\n", capset, cap.get(CV_CAP_PROP_CONVERT_RGB));
	//capset = cap.set(CV_CAP_PROP_FOURCC, CV_FOURCC('V', '4', '2', '2'));
	//printf("CV_CAP_PROP_FOURCC: %d, %d\n", capset, cap.get(CV_CAP_PROP_FOURCC));
	//camera.set(CV_CAP_PROP_MODE, CV_CAP_MODE_YUYV);

	namedWindow("Video");
	setMouseCallback("Video", MouseCallBack, NULL);

	while(c != 27) {
		if (!cap.read(frame))  // read next frame
			break;
		imshow("Video_RGB", frame);

		cvtColor(frame, yuv, CV_BGR2YUV);
		imshow("Video", yuv);

		if (framecnt == 0) printf("width %d; height %d\n", frame.cols, frame.rows);
		printf("frame %d\n", framecnt);
		framecnt++;

		c = waitKey(1000);
		switch (c) {
		case ' ':
			waitKey(0);
			break;
		}
	}

	return 0;
}

